package com.imay.live.activity.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.activity.ProfileActivity;
import com.imay.live.adapter.ImayCelebrityAdapter;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.ImayUserMoreModel;
import com.imay.live.model.UserModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;
import com.imay.tbas_api.IMYTBASObject;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by liangzhenxiong on 2017/04/08 0008.
 * imay红人 红人列表
 */

public class CelebrityActivity extends DicLoadActivity {
    String hotType;
    final static String HOTTYPE = "hotType";
    final static String TITLE = "title";
    private IMYTBASObject mSocketNotify;

    public static void start(Context context, String hottype, String title) {
        Intent starter = new Intent(context, CelebrityActivity.class);
        starter.putExtra(HOTTYPE, hottype);
        starter.putExtra(TITLE, title);
        context.startActivity(starter);
    }

    @Override
    public void onLoadMore() {
    }


    FollowControl followControl;

    @Override
    protected void initAdapte() {
        super.initAdapte();
        mAdapter = new ImayCelebrityAdapter(mdatas);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mrecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .size(0)
                .build());
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_face || view.getId() == R.id.tv_name) {
                    ImayUserMoreModel imayUserMoreModel = (ImayUserMoreModel) mAdapter.getData().get(position);
                    if (imayUserMoreModel != null && imayUserMoreModel.getUser() != null) {
                        ProfileActivity.start(CelebrityActivity.this, imayUserMoreModel.getUser().getUid());
                    }

                } else if (view.getId() == R.id.tv_fllow) {
                    ImayUserMoreModel imayUserMoreModel = (ImayUserMoreModel) mAdapter.getData().get(position);
                    if (imayUserMoreModel != null) {
                        if (followControl == null) {
                            followControl = new FollowControl();
                        }
                        followControl.followChanged(getApplicationContext(), imayUserMoreModel.getUser());
                    }
                }

            }
        });
    }

    @Override
    public void onRefreshLoad() {
        ProtoRequestHelper.task(ProtoHttpFactory.getHotImayMore(hotType)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                Result<ArrayList<ImayUserMoreModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<ImayUserMoreModel>>>() {
                }.getType());
                loadViewControl.refreshLoadView(result.data, result.result);
                loadViewControl.setEnableLoadmore(false);
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);
            }
        });
    }

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        super.initCreate(savedInstanceState);
        hotType = getIntent().getStringExtra(HOTTYPE);
        String title = getIntent().getStringExtra(TITLE);
        if (TextUtils.isEmpty(hotType)) {
            hotType = "red";
        }
        setTitle(title);
        onRefreshLoad();
        registerSocketNotify();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSocketNotify != null) {
            mSocketNotify.destroy();
        }
    }

    private void byUidSetChanged(int uid, final boolean follow) {
        if (mAdapter == null) {
            return;
        }
        ArrayList<Object> list = (ArrayList<Object>) mAdapter.getData();
        for (int i = 0; i < list.size(); i++) {
            Object multiItemEntity = list.get(i);
            if (multiItemEntity instanceof ImayUserMoreModel) {
                ImayUserMoreModel moreModel = (ImayUserMoreModel) multiItemEntity;
                UserModel userModel = moreModel.getUser();
                if (userModel != null) {
                    if (userModel.getUid() == uid) {
                        userModel.setMyFollow(follow);
                        final int finalI = i;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (followControl == null) {
                                    followControl = new FollowControl();
                                }
                                TextView tv_fllow = (TextView) followControl.getViewByPosition(mrecyclerView, finalI, R.id.tv_fllow);
                                if (tv_fllow == null) {
                                    mAdapter.notifyDataSetChanged();
                                } else {
                                    if (follow) {
                                        tv_fllow.setText(getResources().getString(R.string.has_followed));
                                        tv_fllow.setTextColor(getResources().getColor(R.color.text_b2));
                                        tv_fllow.setBackgroundResource(R.drawable.shape_corner5_white_bc);
                                    } else {
                                        tv_fllow.setText(getResources().getString(R.string.un_followed));
                                        tv_fllow.setTextColor(getResources().getColor(R.color.color_ff));
                                        tv_fllow.setBackgroundResource(R.drawable.shape_corner100_white_ff0);
                                    }
                                }

                            }
                        });
                        break;
                    }
                }
            }
        }
    }


    protected void registerSocketNotify() {
        mSocketNotify = new IMYTBASObject(false);
        mSocketNotify.stringCommand = EventHelper.EVENT_FOLLOW_UICHANGE;
        mSocketNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(final IMYTBASObject object) {
                String data = object.stringData;
                int sp = data.indexOf("#");
                if (sp > 0) {
                    String uid = data.substring(0, sp);
                    byUidSetChanged(Integer.valueOf(uid), false);
                } else {
                    byUidSetChanged(Integer.valueOf(data), true);
                }

            }
        });
        mSocketNotify.addEventListenWithTBASObject();
    }

}
